package _Error;

sub option_check($$$){
	my $option_string_addr = $_[0];
	my $flags_addr = $_[1];
	my $ARGV_addr = $_[2];

	my $full_option_string = join(" ",@$ARGV_addr);
	if($full_option_string =~ /--help/){
		die "Usage : $0 [options] interface \n".
			 "	$0 [--range] start end [--no-thread] [--only-ap] [--only-clients] interface \n";
	}

	my $index = 0;
	for($index=0;$index<$#$ARGV_addr;$index++){
		if($$flags_addr{$$ARGV_addr[$index]} =~ 'off'){
			$$flags_addr{$$ARGV_addr[$index]} = "on";
			$$option_string_addr .= "$$ARGV_addr[$index] ";
			if($$ARGV_addr[$index] =~ "--range"){ $index+=2; }
		}else{
			die "Usage : $0 [options] interface \n".
			 	 "	$0 [--range] start end [--no-thread] [--only-ap] [--only-clients] interface \n";
		}	
	}
	
	if($$option_string_addr =~ /--only-ap/ && $$option_string_addr =~ /--only-clients/){
		die "error : option paradox!!! --only-ap -> <- --only-clients ??!!! \n";
	}
	return 0;
}

sub error_check($$){
	$error_check_on = $_[0];
	$error_check_var_addr = $_[1];
	if($error_check_on == -1){
		$error_check_var_addr = $#$error_check_var_addr;
		if($error_check_var_addr < 0){
			return -1;
		}
	}
	elsif($error_check_on == -2){
		if($$error_check_var_addr !~ /\w+\d+/){
			return -2;
		}
	}elsif($error_check_on == -3){
		if($$error_check_var_addr =~ /Device not found/ || $$error_check_var_addr !~ /UP/){
			return -3;
		}
	}elsif($error_check_on == -4){
		($$error_check_var_addr) =~ $$error_check_var_addr =~ /inet addr:([0-9.]*)\s/m;
		if($$error_check_var_addr !~ /192.168.\d{1,3}.\d{1,3}/){
			return -4;
		}
	}elsif($error_check_on == -5){
		if($$error_check_var_addr =~ /not found/ || ($error_check_var_addr =~ /not/ && $error_check_var_addr =~ /found/)){
			return -5;
		}
	}elsif($error_check_on == -6){
		if($$error_check_var_addr[0] !~ /\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}/){ return -6; }
		if($$error_check_var_addr[1] !~ /\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}/){ return -6; }
	}
	return 0;
}

sub print_error($){
	my $err_num_addr = $_[0];
	if($$err_num_addr == -1){
		die "Usage : $0 [options] interface \n".
			 "	$0 [--range] start end [--no-thread] [--only-ap] [--only-clients] interface \n";
	}elsif($$err_num_addr == -2){
		die "error : given interface not correct, usually typoes \n";
	}elsif($$err_num_addr == -3){
		die "error : given interface not found or down, perhaps 'ifconfig interface up'? \n";
	}elsif($$err_num_addr == -4){
		die "error : ip not assigned on given interface \n";
	}elsif($$err_num_addr == -5){
		die "error : dependency error, nmap not found \n";
	}elsif($$err_num_addr == -7){
		die "error : range not correct \n";
	}
}

1;

